package com.hskn.hss.module.userrole.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlresplan.mapper.TlEmergentResplanMapper;
import com.hskn.hss.module.user.entity.User;
import com.hskn.hss.module.userrole.entity.SysUserRole;
import com.hskn.hss.module.userrole.mapper.SysUserRoleMapper;
import com.hskn.hss.module.userrole.service.ISysUserRoleService;
import com.hskn.hss.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 用户角色表 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-04-29
 */
@Slf4j
@RestController
@RequestMapping("/hss/sysuserrole")
public class SysUserRoleController {

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @SysLog(businessType = BusinessType.INSERT, tableName = "sys_user_role", bean = SysUserRole.class, beanMapper = SysUserRoleMapper.class, names = "userid, roleid")
    @RequestMapping(value = "insertUserRole", method = RequestMethod.POST)
    public AjaxResult<Object> insertUserRole(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysUserRoleService.insert(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "sys_user_role", idKey = "id", bean = SysUserRole.class, beanMapper = SysUserRoleMapper.class, names = "userid, roleid")
    @RequestMapping(value = "updateUserRole", method = RequestMethod.POST)
    public AjaxResult<Object> updateUserRole(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysUserRoleService.update(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

//    @SysLog(businessType = BusinessType.UPDATE, idKey = "id", beanMapper = SysUserRoleMapper.class, names = "userid, roleid")
    @RequestMapping(value = "updateUserRoleByUserId", method = RequestMethod.POST)
    public AjaxResult<Object> updateUserRoleByUserId(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysUserRoleService.update(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "sys_user_role", idKey = "userid", bean = SysUserRole.class, beanMapper = SysUserRoleMapper.class, names = "userid, roleid")
    @RequestMapping(value = "deleteUserRoleById", method = RequestMethod.POST)
    public AjaxResult<Object> deleteUserRoleById(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            return sysUserRoleService.deleteByid(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }

    @RequestMapping(value = "getUserRoleById", method = RequestMethod.POST)
    public AjaxResult<Object> getUserRoleById(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        SysUserRole sysUserRole = null;
        try {
            sysUserRole = sysUserRoleService.getInfoById(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
        return AjaxResult.success(sysUserRole);

    }

    @RequestMapping(value = "getUserRoleList", method = RequestMethod.POST)
    public AjaxResult<Object> getUserRoleList(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        List<SysUserRole> roleList = null;
        try {
            roleList = sysUserRoleService.getListByMap(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
        return AjaxResult.success(roleList);
    }

}

